<template>
  <div>
    <el-form ref="form" :model="FEditRole" label-width="80px">
      <el-form-item label="角色名称">
        <el-input v-model="FEditRole.FRoleName"></el-input>
      </el-form-item>
      <el-form-item label="上级角色">
        <el-cascader
          v-model="FEditRole.tempPRoleID"
          :options="FElmDataTree"
          :props="{ checkStrictly: true }"
          filterable
          clearable
          placeholder="单击选择"
          class="width:100%"
        ></el-cascader>
      </el-form-item>
      <el-form-item>
        <el-row type="flex" justify="end">
          <el-switch
            v-model="FEditRole.FStatusCode"
            active-text="启用"
            inactive-text="禁用"
            active-value="启用"
            inactive-value="禁用"
          >
          </el-switch>
        </el-row>
      </el-form-item>
    </el-form>
    <el-row justify="end">
      <el-button type="primary" @click="onSave" :disabled="!zIsOpenData"
        >保存</el-button
      >
      <el-button type="warning" @click="onCancel">取消</el-button>
    </el-row>
  </div>
</template>

<script  lang="ts">
import { TBaseRole } from "@/baselib/right/BaseRightClass";
import { Options } from "vue-class-component";
import TFormBase from "@/views/BaseForm";
import elmHelper, { IElmTreeData } from "../../model/elmHelper";
//配合着用
@Options({
  name: "TFormRoleEdit",
  components: {},
  computed: {},
  data() {},
})
export default class TFormRoleEdit extends TFormBase {
  private FEditRole: TBaseRole = new TBaseRole();
  private FRoleList: TBaseRole[] = [];
  private FElmDataTree: IElmTreeData[] = [];
  //
  public async created() {
    this.FEditRole = this.zContextParams.IndexRole;
    this.FEditRole.tempPRoleID = this.FEditRole.FPRoleID;
    this.FRoleList = this.zContextParams.RoleList;
    //转树形数据
    this.FElmDataTree = elmHelper.dataTreeToElmTree(
      this.FRoleList,
      "FRoleID",
      "children",
      "FRoleName",
      "FRoleID"
    );
    this.openData();
  }
  //打开数据
  public async openData() {
    if (!this.$myStrHelper.stringIsEmpty(this.FEditRole.FRoleID)) {
      //打开数据
      this.zIsOpenData = false;
      let lResult = await this.$myBaseRightAPI.getRole(this.FEditRole.FRoleID);
      if (!lResult.resultSuccess) {
        this.$myMsgHelp.msgDialog(lResult.resultMsg);
        return;
      }
      this.FEditRole = lResult.resultData;
      if (this.$myStrHelper.stringIsEmptyYW(this.FEditRole.FRoleID)) {
        this.$myMsgHelp.msgDialog(
          "返回的数据结构有问题:" + JSON.stringify(lResult.resultData)
        );
        return;
      }
      this.FEditRole.tempPRoleID = this.FEditRole.FPRoleID;
      this.zIsOpenData = true;
    } else {
      this.zIsOpenData = true;
    }
  }
  //
  private async onSave() {
    //当清空是elm返回的是null,多级返回的是数组
    if (this.FEditRole.tempPRoleID == null) {
      this.FEditRole.FPRoleID = "";
    } else if (this.FEditRole.tempPRoleID instanceof Array) {
      let temp =
        this.FEditRole.tempPRoleID[this.FEditRole.tempPRoleID.length - 1];
      if (this.FEditRole.FRoleID == temp) {
        this.FEditRole.tempPRoleID = this.FEditRole.FPRoleID;
      } else {
        this.FEditRole.FPRoleID = temp;
      }
    }
    //
    //保存数据
    let lResult = await this.$myBaseRightAPI.saveRole(this.FEditRole);
    if (!lResult.resultSuccess) {
      this.$myMsgHelp.msgDialog(lResult.resultMsg);
      return;
    }
    this.$myMsgHelp.msgHintAutoClose(
      "保存数据成功",
      this.$myMsgHelp.msgSuccess
    );
    if (this.zContextOnCall != null && this.zContextOnCall != undefined) {
      this.zContextOnCall("save", this.FEditRole);
    }
  }
  private onCancel() {
    if (this.zContextOnCall != null && this.zContextOnCall != undefined) {
      this.zContextOnCall("close", this.FEditRole);
    }
  }
}
</script>
<style>
</style>